#ifndef _PROTOCOL_H_
#define _PROTOCOL_H_

#include <unistd.h>

#ifdef LINUX
#include <netinet/in.h>

#include <sys/socket.h>
#include <sys/time.h>
#include <sys/types.h>
#include <arpa/inet.h>
#endif

#include <stdlib.h>
#include <stdio.h>
#include <string>

#ifdef WII
#include <gccore.h>
#include <network.h>

#include <malloc.h>
#include <ogcsys.h>
#include <gccore.h>
#include <ogc/ipc.h>
#endif

#define MAX_LENGTH 500
#define BUFFER_LEN 256

namespace {
	static const char* UPDATE = "update";
	static const char* GET_UID = "get_uid";
	static const char* UPDATE_NAME = "name";
	static const char* UPDATE_POS = "pos";
	static const char* REQUEST_MAIL = "mail";
	static const char* SEND_MSG = "msg_send";
	static const char* GLOBAL_MESSAGE = "global";
	static const char* REQUEST_USER_LIST = "users";
	static const char* REQUEST_ALL_DATA  = "all_data";
	static const char* QUIT = "quit";
	static const char* SPLIT = "_@@_";

};

namespace MessageType {
      enum Type { NONE, UID_RESPONSE, MAIL_RESPONSE, USER_LIST_RESPONSE, ALL_DATA_RESPONSE };
};

class Protocol {

   public:

      Protocol();
      ~Protocol();


      bool Connect(char* ip, int port);
      void Disconnect();

      bool Update_Name(char* name);
      bool Send_Message(const char* msg, char* name);
      bool Update_Pos(int x, int y, int z);
      bool Rqst_Mail();
      bool Get_Uid();
      bool Rqst_User_List();

      MessageType::Type Rcv_Handler();

      bool Send_Quit();
   
      char* Get_Rcv_Buffer(){return m_buffer;}
      void  Clear_Rcv_Buffer();

      int ParseUid(std::string buff);
      char* ParseName(std::string buff);

      std::string ParseMessages(std::string msg);
      std::string ParseUsers(std::string msg);

   private:
      int m_sock;
      struct sockaddr_in m_server;
      char m_buffer[BUFFER_LEN];
      unsigned int m_msglen;
      char m_message[MAX_LENGTH];
};

#endif //_PROTOCOL_H_
